/*
 * MainPanel.java
 *
 * Created on 24 févr. 2010, 22:47:24
 */
package chipwits;

import chipwits.operator.Arrow;
import chipwits.operator.Op_case;
import chipwits.operator.Operator;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.SwingUtilities;

/**
 *
 * @author Emmanuel
 */
public class MainPanel extends javax.swing.JPanel implements MouseListener, KeyListener {

    final static int nbCasesEnX = 10, nbCasesEnY = 6;
    Op_case opVides[]; //liste des cases d'opérateur.

    //**************   Constructeurs   ****************************
    public MainPanel() {
        super();
        setFocusable(true);
        this.requestFocusInWindow(true);
        addKeyListener(this);
        addMouseListener(this);
        //this.setLayout(new FlowLayout());

        opVides = new Op_case[nbCasesEnX * nbCasesEnY];

        int posX, posY;
        for (int i = 0; i < nbCasesEnX; i++) {
            for (int j = 0; j < nbCasesEnY; j++) {
                posX = i * (Operator.ICON_WIDTH + 15) + 15;
                posY = j * (Operator.ICON_HEIGHT + 15) + 15;
                opVides[i * nbCasesEnY + j] = new Op_case(posX, posY);
                ((javax.swing.JLabel) opVides[i * nbCasesEnY + j]).setBounds(posX, posY, Operator.ICON_WIDTH, Operator.ICON_HEIGHT);
                ((javax.swing.JLabel) opVides[i * nbCasesEnY + j]).setBorder(javax.swing.BorderFactory.createEtchedBorder());
                this.add(opVides[i * nbCasesEnY + j]);
            }
        }
    }


    // <editor-fold defaultstate="collapsed" desc="gestion de l'affichage">
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Arrow.setGraphics(g);       //peut etre à améliorer ....
        for (int i = 0; i < nbCasesEnX; i++) {
            for (int j = 0; j < nbCasesEnY; j++) {
                if (!(opVides[i * nbCasesEnY + j].type.isVIDE())) {         //Si l'opérateur n'est pas vide, on trace les flèches
                    opVides[i * nbCasesEnY + j].redrawArrows();
                }
            }
        }
    }

    public void update() {
        this.repaint();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Evenements souris">
    public void mouseClicked(MouseEvent e) {
    }

    public boolean clicGauche(MouseEvent e){
        if (SwingUtilities.isLeftMouseButton(e)) {
            return true;
        }
        return false;
    }

    public void mousePressed(MouseEvent e) {
        if (Op_case.opSelect != null) {     //si un opcase est sélectionné, on trace les fleches.
            int x = Op_case.opSelect.xpos;
            int y = Op_case.opSelect.ypos;
            if (isMouseInRect(e, x - Arrow.ARROW_LENGTH, y - Arrow.ARROW_LENGTH, x + Arrow.ARROW_LENGTH + Op_case.ICON_WIDTH, y + Arrow.ARROW_LENGTH + Op_case.ICON_HEIGHT)) {
                if (isMouseInRect(e, x - Arrow.ARROW_LENGTH, y, x, y + Op_case.ICON_HEIGHT)) {    //à gauche
                    Op_case.opSelect.setArrow(clicGauche(e), Arrow.Direction.GAUCHE);
                } else if (isMouseInRect(e, x + Op_case.ICON_WIDTH, y, x + Arrow.ARROW_LENGTH + Op_case.ICON_WIDTH, y + Op_case.ICON_HEIGHT)) {    //à droite
                    Op_case.opSelect.setArrow(clicGauche(e), Arrow.Direction.DROITE);
                } else if (isMouseInRect(e, x, y - Arrow.ARROW_LENGTH, x + Op_case.ICON_WIDTH, y)) {    //en haut
                    Op_case.opSelect.setArrow(clicGauche(e), Arrow.Direction.HAUT);
                } else if (isMouseInRect(e, x, y + Op_case.ICON_HEIGHT, x + Op_case.ICON_WIDTH, y + Op_case.ICON_HEIGHT + Arrow.ARROW_LENGTH)) {    //en bas
                    Op_case.opSelect.setArrow(clicGauche(e), Arrow.Direction.BAS);
                }
                Op_case.opSelect.selectArrows(true);
                Op_case.updatePanel();
            }
        }
    }

    public boolean isMouseInRect(MouseEvent e, int x1, int y1, int x2, int y2) {
        if (e.getX() >= x1 && e.getX() <= x2 && e.getY() >= y1 && e.getY() <= y2) {
            return true;
        }
        return false;
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Evenements clavier">
    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        //System.out.println(e.getKeyCode());
        if (e.getKeyCode() == KeyEvent.VK_DELETE && Op_case.opSelect != null) {      //suppressiond e l'opérateur sélectionné.
            Op_case.opSelect.empty();
        }
    }

    public void keyReleased(KeyEvent e) {
    }
    // </editor-fold>
}
